import { createBrowserRouter, json } from "react-router-dom";
import { Layout, Board, Article, NotFound } from "@com/index"

const router = createBrowserRouter([
  {
    path: "/",
    element: <Layout />,
    action: async ({ request, params }) => {
      let formData = await request.formData();
      const data = Object.fromEntries(formData);
      console.log(data);    // {title: '123', description: '123'}
    },
    children: [
      {
        element: <Board />,
        index: true, // index设置为true 变成默认的二级路由
      },
      {
        path: 'board',
        element: <Board />,
        loader: ({ request, params }) => {
          const url = new URL(request.url);
          // 请求为 http://localhost:5173/board?id=1
          const searchTerm = url.searchParams.get("id");  // 1
        },
      },
      {
        path: 'board/:id',
        element: <Board />,
        loader: ({ request, params }) => {
        }
      },
      {
        path: 'article',
        element: <Article />,
      }
    ]
  },
  {
    path: "*",
    element: <NotFound />,
  }
])

export default router
